import serviceConfig from '@/plugins/axios/Config'
import { ElckerAxiosRequest } from './types';
import store from '@/store/index'
import { ElNotification, ElMessageBox, ElMessage, ElLoading } from 'element-plus'
class ElckerServiceApi {
    // 自定义header头
    protected headers: any = {
        'Content-Type': 'application/json;charset=UTF-8'
    }
    protected apiSate?: boolean = process.env.VUE_APP_APISATE == "true" ? true : false;
    private apiAxios({ headers = this.headers, method, url, data, params, responseType, auth, apiSate = process.env.VUE_APP_APISATE == "true" }: ElckerAxiosRequest): any {
        // 1.用户登录配置
        if (process.env.VUE_APP_URL_LOGIN != url) {
            // 判断是否登录 没有跳转到登录页面
            if (store.getters.isLogin == true) {
                // 是否需要设置 token
                //const isToken = (config.headers || {}).isToken === false
                const token = store.getters.getToken;
                if (token == null || token == "") {
                    ElMessageBox.alert('您尚未登录，请前往登录后再进行此操作！', '系统提示', {
                        confirmButtonText: '确定',
                        callback: function () {
                            store.dispatch('UserLogout');
                        }
                    });
                    return;
                } else {
                    headers['Authorization'] = 'Bearer ' + token // 让每个请求携带自定义token 请根据实际情况自行修改
                    //if (config.method === 'post' || config.method === 'put') {
                    //post请求时，序列化入参
                    //config.data = QS.stringify(config.data);
                    //}
                    return serviceConfig({
                        headers,
                        method,
                        url,
                        params,
                        data,
                        responseType,
                        auth
                    })
                }
            } else {
                // 跳转到登录页面
                ElMessageBox.alert('您尚未登录，请前往登录后再进行此操作！', '系统提示', {
                    confirmButtonText: '确定',
                    callback: function () {
                        store.dispatch('UserLogout');
                    }
                });
                return;
            }
        } else {
            return serviceConfig({
                headers,
                method,
                url,
                params,
                data,
                responseType,
                auth
            })
        }
    }
    /**
         * POST类型的网络请求
         */
    protected LoginPostRequest({ headers, url, data, params, responseType, auth, apiSate }: ElckerAxiosRequest) {
        return this.apiAxios({ headers, method: 'POST', url, data, params, responseType, auth, apiSate });
    }

    /**
     * GET类型的网络请求
     */
    protected getRequest({ headers, url, data, params, responseType, apiSate }: ElckerAxiosRequest) {
        return this.apiAxios({ headers, method: 'GET', url, data, params, responseType, apiSate });
    }

    /**
     * POST类型的网络请求
     */
    protected postRequest({ headers, url, data, params, responseType, apiSate }: ElckerAxiosRequest) {
        return this.apiAxios({ headers, method: 'POST', url, data, params, responseType, apiSate });
    }

    /**
     * PUT类型的网络请求
     */
    protected putRequest({ headers, url, data, params, responseType, apiSate }: ElckerAxiosRequest) {
        return this.apiAxios({ headers, method: 'PUT', url, data, params, responseType, apiSate });
    }

    protected patchRequest({ headers, url, data, params, responseType, apiSate }: ElckerAxiosRequest) {
        return this.apiAxios({ headers, method: 'PATCH', url, data, params, responseType, apiSate });
    }

    /**
     * DELETE类型的网络请求
     */
    protected deleteRequest({ headers, url, data, params, responseType, apiSate }: ElckerAxiosRequest) {
        return this.apiAxios({ headers, method: 'DELETE', url, data, params, responseType, apiSate });
    }

}

export default ElckerServiceApi
